package com.gf.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class MpGenerator {

    public static void main(String[] args) throws InterruptedException {
        // 包名
        String packageName = "com.gf";
        generateByTables(packageName, "tbl_employee");
    }

    /**
     * 生成模板代码
     *
     * @param packageName 包名
     * @param tableNames  表名称
     */
    private static void generateByTables(String packageName, String... tableNames) {
        String dbUrl = "jdbc:mysql://localhost:3306/mp?useSSL=false";
        // 数据库相关配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("root")
                .setDriverName("com.mysql.jdbc.Driver");

        // 生成策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(false) //大写命名
                .setEntityLombokModel(false) // lombok支持
                // 表名称，字段名使用下划线
                .setTablePrefix("tbl") // 表前缀
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityTableFieldAnnotationEnable(true) // tableField注解
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        // 全局配置
        GlobalConfig config = new GlobalConfig();
        config.setAuthor("liulonglong")
                .setOutputDir("d:\\codeGen")
                .setBaseResultMap(true) // base result map
                .setBaseColumnList(true) // base column list
                .setDateType(DateType.ONLY_DATE) //java.util.Date类型
                .setEntityName("%sEntity")
                .setMapperName("%sMapper")
                .setIdType(IdType.AUTO) // 主键自增
                .setFileOverride(true)
                .setServiceName("%sService");
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override public void initMap() {

            }
        };
        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setController("controller")
                .setParent(packageName)
                .setEntity("entity");
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig).execute();
    }
}

